package com.example.utils;

import org.springframework.stereotype.Component;

@Component
public class PriceUtils {
    // 距离区间常量（单位：km）
    private static final int DISTANCE_LOCAL = 50;      // 本地区域
    private static final int DISTANCE_PROVINCE = 300;  // 省内区域
    private static final int DISTANCE_REGION = 800;    // 区域范围
    private static final int DISTANCE_REMOTE = 2000;   // 偏远地区

    // 首重价格（元/kg）
    private static final double PRICE_FIRST_LOCAL = 8.0;    // 本地首重
    private static final double PRICE_FIRST_PROVINCE = 10.0; // 省内首重
    private static final double PRICE_FIRST_REGION = 12.0;   // 区域首重
    private static final double PRICE_FIRST_REMOTE = 15.0;   // 偏远首重

    // 续重价格系数（元/kg）
    private static final double RATE_1_20 = 1.0;    // 1-20kg
    private static final double RATE_20_50 = 1.5;   // 20-50kg
    private static final double RATE_50_100 = 1.2;  // 50-100kg

    /**
     * 计算物流价格
     * @param weight 重量（kg）
     * @param distance 距离（km）
     * @return 物流价格
     */
    public double calculatePrice(double weight, double distance) {
        double basePrice = getBasePrice(distance);
        return calculateTotalPrice(weight, basePrice, distance);
    }

    /**
     * 获取基础价格（首重价格）
     * @param distance 距离（km）
     * @return 基础价格
     */
    private double getBasePrice(double distance) {
        if (distance <= DISTANCE_LOCAL) {
            return PRICE_FIRST_LOCAL;
        } else if (distance <= DISTANCE_PROVINCE) {
            return PRICE_FIRST_PROVINCE;
        } else if (distance <= DISTANCE_REGION) {
            return PRICE_FIRST_REGION;
        } else {
            return PRICE_FIRST_REMOTE;
        }
    }

    /**
     * 计算总价格
     * @param weight 重量（kg）
     * @param basePrice 基础价格
     * @param distance 距离（km）
     * @return 总价格
     */
    private double calculateTotalPrice(double weight, double basePrice, double distance) {
        if (weight <= 1) {
            return basePrice;
        }

        double price = basePrice;
        double remainingWeight = weight - 1; // 减去首重

        // 1-20kg
        if (remainingWeight > 0) {
            double tier1Weight = Math.min(remainingWeight, 19); // 最多19kg (1-20)
            price += tier1Weight * RATE_1_20;
            remainingWeight -= tier1Weight;
        }

        // 20-50kg
        if (remainingWeight > 0) {
            double tier2Weight = Math.min(remainingWeight, 49); // 最多49kg (20-50)
            price += tier2Weight * RATE_20_50;
            remainingWeight -= tier2Weight;
        }

        // 50-100kg
        if (remainingWeight > 0) {
            double tier3Weight = Math.min(remainingWeight, 99); // 最多99kg (50-100)
            price += tier3Weight * RATE_50_100;
        }

        // 根据距离调整价格系数
        double distanceMultiplier = getDistanceMultiplier(distance);
        return price * distanceMultiplier;
    }

    /**
     * 获取距离调整系数
     * @param distance 距离（km）
     * @return 距离调整系数
     */
    private double getDistanceMultiplier(double distance) {
        if (distance <= DISTANCE_LOCAL) {
            return 1.0;
        } else if (distance <= DISTANCE_PROVINCE) {
            return 1.2;
        } else if (distance <= DISTANCE_REGION) {
            return 1.5;
        } else {
            return 2.0;
        }
    }
}
